# -*- coding: UTF-8 -*-
import random
import numpy as np
import sys
sys.path.append('..')
import config
from taxi_special import generateNode,square


def init_node_list_hetero():
    node_list = []
    node_id = 0
    lat_min,lon_min,lat_max,lon_max = square()
    e_loc,e_loc_lst = generateNode(lat_min,lon_min,lat_max,lon_max)

    temp = random.randint(0,config.EDGE_NODE_NUM)
    # cpu 3.5G 左右大概64核 224=64*3.5
    for _ in range(temp):  # for hetero
        radm_layeridlst = random.sample(range(config.LAYER_TOTAL_NUM),random.randint(0,int(config.LAYER_TOTAL_NUM/100)))
        node_list.append(Node(node_id, [96, 256,random.randint(config.node_min_bandwidth, config.node_max_bandwidth),random.randint(config.node_min_disk, config.node_max_disk), random.randint(config.node_min_cpu_freq, config.node_max_cpu_freq),random.randint(config.node_min_wire_bandwidth, config.node_max_wire_bandwidth)], [e_loc_lst[node_id][0],e_loc_lst[node_id][1],radm_layeridlst]))
        node_id += 1
    for _ in range(int(config.EDGE_NODE_NUM-temp)):  
        radm_layeridlst = random.sample(range(config.LAYER_TOTAL_NUM),random.randint(0,int(config.LAYER_TOTAL_NUM/100)))
        node_list.append(Node(node_id, [128, 384,random.randint(config.node_min_bandwidth, config.node_max_bandwidth),random.randint(config.node_min_disk, config.node_max_disk), random.randint(2.0, 4.0), random.randint(config.node_min_wire_bandwidth, config.node_max_wire_bandwidth)], [e_loc_lst[node_id][0],e_loc_lst[node_id][1],radm_layeridlst]))
        node_id += 1
   
    return node_list


class Node:

    def __init__(self, node_id =None, resource_capacity=None, other_feature = None):

        self.id = node_id

        self.cpu = int(resource_capacity[0])#G
        self.mem = int(resource_capacity[1])#G
        self.node_bw = int(resource_capacity[2]) #Mb
        self.disk = int(resource_capacity[3]) # MB
        self.cpu_frequency = int(resource_capacity[4])#
        # For wireless bandwidth
        self.node_bw2 = int(resource_capacity[5])

        self.location_x = other_feature[0]
        self.location_y = other_feature[1]

        self.container_running_list = None 
        self.ini_lyerid = other_feature[2]

        self.available_cpu = self.cpu
        self.available_mem = self.mem
        self.available_disk = self.disk
        self.available_bandwidth = self.node_bw


        self.layer_01_list = np.zeros(config.LAYER_TOTAL_NUM)
        self.image_01_list = None
        self.container_number = 0
        self.download_finish_time = 0
        self.cpu_capacity = self.cpu * 1e9 # GHZ -> HZ = circle/s

    def update_resource(self,task,flag):
        if flag == 'start':
            self.available_cpu -= task.task_cpu
            self.available_cpu -= task.task_cpu
            self.container_running_list[task.requested_image_id] -= 1
            self.container_number += 1
        elif flag == 'end':
            self.available_cpu += task.task_cpu
            self.available_cpu += task.task_cpu
            self.container_running_list[task.requested_image_id] += 1
            self.container_number -= 1
        else:
            raise EnvironmentError

    
if __name__=='__main__':
    # check
    pass

